package me.zhengjie.modules.web.rest.identity;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.zhengjie.config.entity.ResponseBaseEntity;
import me.zhengjie.config.enums.AtmrErrorEnum;
import me.zhengjie.config.exception.WeChatException;
import me.zhengjie.modules.web.domain.identity.IdentityDetailRequest;
import me.zhengjie.modules.web.domain.identity.IdentityListResp;
import me.zhengjie.modules.web.service.identity.IdentityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("web/identity")
@Api(tags = "前端接口-身份产品")
public class IdentityController {

    @Autowired
    private IdentityService identityService;

    @PostMapping("list")
    @ApiOperation("前端接口-身份产品列表")
    public ResponseBaseEntity list(){
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            List<IdentityListResp> list = identityService.list();
            response.setBody(list);
        }catch (WeChatException e){
            response.getHeader().setErrCode("-1");
            response.getHeader().setErrMsg(e.getMsg());
        }catch (Exception e){
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }

    @PostMapping("detail")
    @ApiOperation("前端接口-身份产品详情")
    public ResponseBaseEntity detail(@RequestBody IdentityDetailRequest request){
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            IdentityListResp detail = identityService.detail(request.getItemNo());
            response.setBody(detail);
        }catch (WeChatException e){
            response.getHeader().setErrCode("-1");
            response.getHeader().setErrMsg(e.getMsg());
        }catch (Exception e){
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }
}